package com.aiyige.page.my.customer.adapter;

import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.page.my.customer.model.CustomerEntity;
import com.aiyige.page.my.customer.model.CustomerType;
import com.aiyige.utils.GlideUtil;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.TimeUtils;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xiachao
 */

public class CustomerManageListAdapter extends BaseQuickAdapter<CustomerEntity, BaseViewHolder> {



    public CustomerManageListAdapter() {
        super(new LinkedList<CustomerEntity>());
    }


    @Override
    protected void convert(BaseViewHolder helper, CustomerEntity item) {
        ((MyHolder) helper).bindData(item);
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder = new MyHolder(getItemView(R.layout.item_customer_list, parent));
        return baseViewHolder;
    }


    public void onViewClicked() {

    }



    public class MyHolder extends BaseViewHolder {

        @BindView(R.id.iv_head)
        ImageView ivHead;
        @BindView(R.id.iv_phone)
        ImageView ivPhone;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_transaction_value)
        TextView tvTransactionValue;
        @BindView(R.id.tv_client_type)
        TextView tvClientType;
        @BindView(R.id.tv_last_follow_up_value)
        TextView tvLastFollowUpValue;
        @BindView(R.id.tv_next_follow_up_value)
        TextView tvNextFollowUpValue;
        @BindView(R.id.rl_item_selection)
        RelativeLayout rlItemSelection;
        @BindView(R.id.tv_add_follow_up)
        TextView tvAddFollowUp;
        @BindView(R.id.rl_add_follow_up)
        RelativeLayout rlAddFollowUp;

        public MyHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }


        public void bindData(final CustomerEntity data) {
            Log.v("xiachaocc",""+data.getName());
            rlItemSelection.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ARouter.getInstance().build(ARouterConfig.CustomerDetailPage)
                            .withParcelable("customer", data).navigation();
                }
            });
            ivHead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ARouter.getInstance().build(ARouterConfig.CustomerDetailPage)
                            .withParcelable("customer",data).navigation();
                }
            });
            if (!TextUtils.isEmpty(data.getPhone())) {
                ivPhone.setVisibility(View.VISIBLE);
                ivPhone.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String phone = data.getPhone();
                        if (!TextUtils.isEmpty(phone)) {
                            Intent intent1 = new Intent(Intent.ACTION_DIAL);
                            intent1.setData(Uri.parse("tel:" + phone));
                            mContext.startActivity(intent1);
                        }
                    }
                });
            } else {
                ivPhone.setVisibility(View.INVISIBLE);
            }

            tvAddFollowUp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ARouter.getInstance().build(ARouterConfig.AddFollowUpPage)
                            .withParcelable("customer",data).navigation((BaseActivity)mContext,2);
                }
            });
            GlideUtil.with(mContext).small().loadAvatar(data.getAvatar()).into(ivHead);
            Spanned result;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                result = Html.fromHtml(data.getName(), Html.FROM_HTML_MODE_LEGACY);
            } else {
                result = Html.fromHtml(data.getName());
            }
            tvName.setText(result);
            tvTransactionValue.setText(StringUtils.priceFormat(data.getTotalTransactionPrice())+mContext.getResources().getString(R.string.yuan)+"/"
            +data.getTotalTransactionOrderCount()+mContext.getResources().getString(R.string.bill));
            tvClientType.setText(CustomerType.getName(data.getCustomerType()));
            long lastFollowTime = Long.parseLong(data.getFollowupTime());
            if (lastFollowTime < 1000) {
                tvLastFollowUpValue.setText("");
            } else {
                tvLastFollowUpValue.setText(TimeUtils.dateToYYYYMMDD(lastFollowTime));
            }
            long nextFollowTime = Long.parseLong(data.getNextFollowupTime());
            if (nextFollowTime < 1000) {
                tvNextFollowUpValue.setText("");
            } else {
                tvNextFollowUpValue.setText(TimeUtils.dateToYYYYMMDD(nextFollowTime));
            }
        }
    }

}
